package com.caow.composebase.utils

import android.widget.Toast
import androidx.annotation.MainThread
import com.caow.composebase.GlobalConfig
import java.lang.ref.WeakReference

/**
 * @Author caow
 * 2023/3/14
 */
object ToastUtils {
    private var sToast: WeakReference<Toast>? = null

    /** Shows the toast message after canceling the previous one.  */
    @MainThread
    fun show(text: CharSequence? = null, duration: Int = Toast.LENGTH_SHORT) {
        if (sToast != null && sToast!!.get() != null) {
            sToast?.get()?.cancel()
        }
        val toast: Toast = Toast.makeText(GlobalConfig.app, text, duration)
        toast.show()
        sToast = WeakReference(toast)
    }
}